<?php
namespace App\Models\Traits\Admin;


trait ActionButtonTrait
{
    /**
     * 修改按钮
     * @param $actionModel string 模型
     * @return string
     */

    public function editButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".edit")){
            return "<a href=". route('admin.'.$this->actionUri.'.edit', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-info'><i title='编辑' class='fas fa-edit'></i> 编辑</button></a> ";
        }
        return '';
    }

    /**
     * 删除按钮
     * @param  [type] $actionModel [description]
     * @param  [type] $id          [description]
     * @param  [type] $user_id     [description]
     * @return [type]              [description]
     */
    public function deleteButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".destroy")){
            $button  = "";
            $button .= "<form id='deleteForm' action='". route('admin.'.$this->actionUri.'.destroy', $this->id) ."' method='POST'  style='display: inline-block;' >";
            $button .= method_field('DELETE').csrf_field();
			$button .= "<button @click='del(".$this->id.")' type='button' actionstr='". route('admin.'.$this->actionUri.'.destroy', $this->id) ."' ids='".$this->id."' class='btn btn-sm waves-effect waves-light btn-danger isDel'>
             <i class='fas fa-trash-alt'></i> 删除</button>";
            $button .= '</form> ';
            return $button;
        }
        return '';
    } 


    // public function deleteButton()
    // {
    //     //if (auth('admin')->user()->can("admin.". $this->actionUri .".delete")){
    //         $button  = "";
    //         $button .= "<form id='deleteForm' action='". route('admin.'.$this->actionUri.'.destroy', $this->id) ."' method='POST'  style='display: inline-block;' >";
    //         $button .= method_field('DELETE').csrf_field();
    //         $button .= '<button onclick="del()" urls="'.route('admin.'.$this->actionUri.'.destroy', $this->id).'" type="button" class="btn btn-sm waves-effect waves-light btn-danger isdel">
    //               <i class="fas fa-trash-alt"></i> 删除
    //             </button>';
    //         $button .= '</form> ';
    //         return $button;
    //     //}
    //     return '';
    // }
    /**
     * [详情按钮]
     * @return [type] [description]
     */
    public function showButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".show")){
            return "<a href=". route('admin.'.$this->actionUri.'.show', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-secondary'><i title='详情' class='fas fa-file-alt'></i> 详情</button></a> ";
        }
        return '';
    } 
    
   
    public function configButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".config")){
            return "<a href=". route('admin.'.$this->actionUri.'.config', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='参数配置' class='mdi mdi-settings'></i> 参数配置</button></a> ";
        }
        return '';
    } 

    //分配权限按钮
    public function setPermissionsButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".setPermissions")){
            return "<a href=". route('admin.'.$this->actionUri.'.setPermissions', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='分配权限' class='mdi mdi-settings'></i> 分配权限</button></a> ";
        }
        return '';
    }

    //分配设备按钮
    public function allocationButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".allocation")){
            return "<a href=". route('admin.'.$this->actionUri.'.allocation', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='分配设备' class='mdi mdi-settings'></i> 分配设备</button></a> ";
        }
        return '';
    }
    //分配角色
    public function setRoleButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".setRole")){
            return "<a href=". route('admin.'.$this->actionUri.'.setRole', $this->id) ."><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='分配角色' class='mdi mdi-settings'></i> 分配角色</button></a> ";
        }
        return '';
    }
    //重置密码
    public function resetPassword()
    {
        if(auth('admin')->user()->can("admin.". $this->actionUri .".resetPassword")){
            return "<a href='javascript:;' @click='reset(".$this->id.")'><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='重置密码' class='mdi mdi-settings'></i> 重置密码</button></a> ";
        }
        return '';
    }

    //发布公告
    public function publishNoticeButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".publishNotice")){
            return "<a href='javascript:;' @click='publishNotice(".$this->id.")'><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='发布公告' class='mdi mdi-settings'></i> 发布公告</button></a> ";
        }
        return '';
    }

    //设备重置
    public function resetMachineButton()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".resetMachine")){
            return "<a href='javascript:;' @click='resetMachine(".$this->id.")'><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='初始化' class='fas fa-redo'></i> 初始化</button></a> ";
        }
        return '';
    }
    //订单退款
    public function orderRefund()
    {
        if (auth('admin')->user()->can("admin.". $this->actionUri .".refund")){
            return "<a href='javascript:;' @click='orderRefund(".$this->id.")'><button type='button' class='btn btn-sm waves-effect waves-light btn-success'><i title='退款' class='fas fa-redo'></i> 退款</button></a> ";
        }
        return '';
    }
    /**
     *
     */
    public function getActionButtonAttribute(){
        $html = '';
        foreach ($this->actions as $value) {
            switch ($value) {
                case 'add':
                    $html .= $this->addButton();
                    break;
                case 'edit':
                    $html .= $this->editButton();
                    break;
                case 'delete':
                    $html .= $this->deleteButton();
                    break;
                case 'show':
                    $html .= $this->showButton();
                    break;
                case 'config':
                    $html .= $this->configButton();
                    break;
                case 'set_permissions':
                    $html .= $this->setPermissionsButton();
                    break;
                case 'allocation':
                    $html .= $this->allocationButton();
                    break;
                case 'set_role':
                    $html .= $this->setRoleButton();
                    break;
                case 'resetPassword':
                    $html .= $this->resetPassword();
                    break;
                case 'publishNotice':
                    $html .= $this->publishNoticeButton();
                    break;
                case 'resetMachine':
                    $html .= $this->resetMachineButton();
                    break; 
                case 'refund':
                    $html .= $this->orderRefund();
                    break;    
            }
        }
        return $html;
    }
}